﻿using Abp.Linq.Expressions;
using Entity.Models.dto;
using Entity.Models.Entities;
using Entity.Models.vo;
using Entity.Models.vo.Menu;
using IBLL;
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using utils.enums;
using utils.Enums;
using utils.EX;
using Utils;

namespace BLL
{
    /// <summary>
    /// 菜单BLL层
    /// </summary>
    public class MenuInfoBLL : IMenuInfoBLL
    {
        /// <summary>
        /// 菜单DAL接口
        /// </summary>
        IMenuInfoDAL _menuInfoDal;

        /// <summary>
        /// 用户接口
        /// </summary>
        IUserDAL _userDAL;

        /// <summary>
        /// 角色接口
        /// </summary>
        IR_UserInfo_RoleInfoDAL _r_UserInfo_RoleInfoDAL;

        /// <summary>
        /// 菜单
        /// </summary>
        IR_RoleInfo_MenuInfoDAL _r_RoleInfo_MenuInfoDAL;

        public MenuInfoBLL(IMenuInfoDAL menuInfo, IUserDAL userDAL, IR_UserInfo_RoleInfoDAL r_UserInfo_RoleInfoDAL, IR_RoleInfo_MenuInfoDAL r_RoleInfo_MenuInfoDAL)
        {
            _menuInfoDal = menuInfo;
            _userDAL = userDAL;
            _r_UserInfo_RoleInfoDAL = r_UserInfo_RoleInfoDAL;
            _r_RoleInfo_MenuInfoDAL = r_RoleInfo_MenuInfoDAL;
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menuInfo"></param>
        /// <returns></returns>。
        public bool AddMenuInfo(MenuInfo menuInfo)
        {
            menuInfo.Id = Guid.NewGuid().ToString();
            menuInfo.CreateTime = DateTime.Now;
            //查询
            MenuInfo bymenuInfoInfo = _menuInfoDal.getEntitys().FirstOrDefault(d => d.IsDelete != true && d.Title == menuInfo.Title);
            if (bymenuInfoInfo == null)
            {
                //保存
                if (_menuInfoDal.AddEntity(menuInfo) < 0)
                {
                    throw new MyException(((int)UserEnums.USER_STATE.LOSE), Enums.getDescribe(UserEnums.USER_STATE.LOSE));
                }
            }
            else
            {
                throw new MyException(2112, "菜单已存在");
            }
            return true;
        }

        /// <summary>
        /// 获取首页菜单栏列表
        /// </summary>
        /// <returns></returns>
        public MenuInfoVo getMenuList(string userId)
        {
            //根据用户id获取角色id
            List<string> rpleIds = _r_UserInfo_RoleInfoDAL.getEntitys().Where(u => u.UserId.EndsWith(userId)).Select(u => u.RoleId).ToList();
            //根据角色id获取菜单
            List<string> menuIds = _r_RoleInfo_MenuInfoDAL.getEntitys().Where(m => rpleIds.Contains((m.RoleId))).Select(m => m.MenuId).ToList();

            //获取当前用户能看菜单
            List<MenuInfo> datas = _menuInfoDal.getEntitys().Where(d => d.IsDelete != true && menuIds.Contains(d.Id)).ToList();

            //获取顶级菜单
            List<MenuInfos> menuInfos = _menuInfoDal.getEntitys().Where(m => m.Level == 0).OrderBy(d => d.Sort).Select(m => new MenuInfos(m)).ToList();

            //数据集
            MenuInfoVo menuInfoVo = new MenuInfoVo();
      
            //查询子菜单
            getChild(menuInfos, datas);
            menuInfoVo.menuInfo = menuInfos;
            return menuInfoVo;
        }

        /// <summary>
        /// 查询子菜单
        /// </summary>
        /// <param name="parentMenu"></param>
        /// <param name="datas"></param>
        public void getChild(List<MenuInfos> parentMenu,List<MenuInfo> datas)
        {
            foreach (var item in parentMenu)
            {
                List<MenuInfos> infos= datas.Where(m => m.ParentId.EndsWith(item.Id) ).OrderBy(d => d.Sort).Select(m => new MenuInfos(m)).ToList();
                //递归查询
                getChild(infos, datas);
                item.child = infos;
            }
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="menuInfoId">菜单id</param>
        /// <returns></returns>
        public bool DalateMenuInfo(string menuInfoId)
        {
            //删除
            if (_menuInfoDal.SoftDalateEntity(menuInfoId) < 0)
            {
                throw new MyException(((int)UserEnums.USER_STATE.DATATE_LOSE), Enums.getDescribe(UserEnums.USER_STATE.DATATE_LOSE));
            }
            return true;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="menuInfoIds"></param>
        /// <returns></returns>
        public bool DalateMenuInfoList(List<String> menuInfoIds)
        {
            if (menuInfoIds.Count < 1)
            {
                throw new MyException(((int)UserEnums.USER_STATE.UPDATE_LIST), Enums.getDescribe(UserEnums.USER_STATE.UPDATE_LIST));
            }
            foreach (var item in menuInfoIds)
            {
                DalateMenuInfo(item);
            }
            return true;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="menuInfo"></param>
        /// <returns></returns>
        public bool UpdataMenuInfo(MenuInfo menuInfo)
        {
            if (_menuInfoDal.UpdataEntity(menuInfo) < 0)
            {
                throw new MyException(((int)UserEnums.USER_STATE.UPDATE_LOSE), Enums.getDescribe(UserEnums.USER_STATE.UPDATE_LOSE));
            }
            return true;
        }

        /// <summary>
        /// 根据菜单id搜索
        /// </summary>
        /// <param name="menuInfoid"></param>
        /// <returns></returns>
        public MenuInfo getByMenuInfo(string menuInfoid)
        {
            return _menuInfoDal.getByEntityId(menuInfoid);
        }

        /// <summary>
        /// 数据列表、搜索
        /// </summary>
        /// <param name="titleName"></param>
        /// <returns></returns>
        public List<MenuTreetableVo> getMenuTreetab(string titleName)
        {
            List<MenuTreetableVo> menuTreetables = new List<MenuTreetableVo>();

            IQueryable<MenuInfo> datas = _menuInfoDal.getEntitys().Where(d => d.IsDelete != true).OrderBy(d => d.Sort);
            if (!string.IsNullOrEmpty(titleName))
            {
                datas = datas.Where(i => i.Title.Contains(titleName));
            }
            //获取所以菜单
            menuTreetables = datas.Select(m => new MenuTreetableVo(m)).ToList();

            return menuTreetables;
        }


        /// <summary>
        /// 获取菜单下拉选数据
        /// </summary>
        /// <returns></returns>
        public List<TransferVo> getMenuInfoSpinner()
        {
            return _menuInfoDal.getEntitys().Where(u => u.IsDelete != true).Select(u => new TransferVo(u)).ToList();
        }
    }
}
